<?php

namespace app\api\controller;

use app\common\controller\Common;
use think\Db;

class Scene extends Common{
    //
    public function index(){
        try{
            $result = Db::name('scenes')->select();
            return resultArray(['data' => $result]);
        }catch(\Exception $e){
            return resultArray(['error' => '查询错误']);
        }
    }

    //创建
    public function store(){
        try{
            $param = [
                'scene' => input('param.scene'),
                'name' => input('param.name'),
            ];
            $reuslt = Db::name('scenes')->insert($param);
            if(!$reuslt){
                return resultArray(['error' => '添加失败']);
            }
            return resultArray(['data' => '']);
        }catch(\Exception $e){
            return resultArray(['error' => '添加失败']);
        }
    }

    //更新
    public function update(){
        try{
            $param = input('param.');
            if(!$param['id']){
                return resultArray(['error' => '未获取到id']);
            }
            unset($param['token']);
            $result = Db::name('scenes')->where('id', $param['id'])->update($param);
            return resultArray(['data' => '']);
        }catch(\Exception $e){
            //return resultArray(['error' => $e->getMessage()]);
            return resultArray(['error' => '更新失败']);
        }
    }

    //详情
    public function show($id){
        try{
            $result = Model('Scenes')->with('hotword')->find($id);
            if($result){
                if($result['hotword']){
                    $list = [];
                    foreach(json_decode($result['hotword']['content'], true) as $str){
                        $list[]['name'] = $str;
                    }
                    $result['hotword']['content'] = $list;
                    //$tmp = array('A'=>[],'B'=>[],'C'=>[["name" => "吃饭"]],'D'=>[["name" => "打豆豆"]],'E'=>[],'F'=>[],'G'=>[],'H'=>[],'I'=>[],'J'=>[],'K'=>[],'L'=>[],'M'=>[],'N'=>[],'O'=>[],'P'=>[],'Q'=>[],'R'=>[],'S'=>[["name" => "睡觉"]],'T'=>[],'U'=>[],'V'=>[],'W'=>[],'X'=>[],'Y'=>[],'Z'=>[],'#'=>[]);
                    //$result['hotword']['content'] = $tmp;
                }
                return resultArray(['data' => $result]);
            }
            return resultArray(['data' => '']);
        }catch(\Exception $e){
            return resultArray(['error' => $e->getMessage()]);
        }
    }
}